package com.breaktian.assemble.utils;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by zerup on 17/4/17.
 */

public class ThreadUtil {
    static Handler mMainHandler;
    static Handler mHelpHandler;
    static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

    public static void run(Runnable runnable) {
        cachedThreadPool.execute(runnable);
    }

    public synchronized static Handler getMainHandler() {
        if (mMainHandler == null) {
            mMainHandler = new Handler(Looper.getMainLooper());
        }
        return mMainHandler;
    }

    public static void runOnUiThread(Runnable runnable){
        getMainHandler().post(runnable);
    }
    public static void runOnUiThread(Runnable runnable,long delay){
        getMainHandler().postDelayed(runnable,delay);
    }

    public synchronized static Handler getHelpHandler() {
        if (mHelpHandler == null) {
            HandlerThread thread = new HandlerThread("help_thread");
            thread.start();
            mHelpHandler = new Handler(thread.getLooper());
        }
        return mHelpHandler;
    }
}
